﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using NextCMS.Models;

/// <summary>
/// Summary description for SiteSettingsController
/// </summary>
public class SiteSettingsHelper : ISiteSettings
{
    ISiteSettings siteSettings = null;
    public SiteSettingsHelper()
    {
        siteSettings = (ISiteSettings)NextCMS.DataAccess.Entities.CreateInstance<ISiteSettings>();
    }
    public static string GetSetting(string Key,int SiteId)
    {
        return ApplicationCache.SiteSettings.Where(c => c.Key == Key && c.SiteId == SiteId).Select(c => c.Value).FirstOrDefault();
    }
    
    public int Save(SiteSettingsModel page)
    {
        int id= siteSettings.Save(page);
        ApplicationCache.SiteSettings = null;
        return id;
    }
    // Delete Record
    public void Delete(int id)
    {
        siteSettings.Delete(id);
        ApplicationCache.SiteSettings = null;
    }

    // Edit Record
    public static void SetSetting(string Key,string Value,int SiteId)
    {
        using (ISiteSettings siteSettings = (ISiteSettings)NextCMS.DataAccess.Entities.CreateInstance<ISiteSettings>())
        {
            siteSettings.Edit(Key, Value, SiteId);
        }
        ApplicationCache.SiteSettings = null;
    }

    public void Dispose()
    {
        siteSettings.Dispose();
        siteSettings = null;
    }



    public IEnumerable<SiteSettingsModel> GetAll()
    {
        return siteSettings.GetAll();
    }

    public string GetByKey(string Key)
    {
        return ApplicationCache.SiteSettings.Where(c => c.Key == Key && c.SiteId == 0).Select(c => c.Value).FirstOrDefault();
    }

    public string GetByKey(string Key, int SiteId)
    {
        return ApplicationCache.SiteSettings.Where(c => c.Key == Key && c.SiteId == SiteId).Select(c => c.Value).FirstOrDefault();
    }

    public void Edit(string Key, string Value, int SiteId)
    {
        siteSettings.Edit(Key, Value, SiteId);
        ApplicationCache.SiteSettings = null;
    }
}